package com.lam.system.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.lam.common.base.controller.BaseController;
import com.lam.common.base.domain.Result;
import com.lam.common.components.dict.annotation.DictAutoTransfer;
import com.lam.common.constant.SystemConstant;
import com.lam.common.validate.ValidateUtil;
import com.lam.common.validate.group.Add;
import com.lam.common.validate.group.Update;
import com.lam.common.validate.model.ValidOperType;
import com.lam.framework.annotation.ParamInject;
import com.lam.framework.bean.entity.SysDictItem;
import com.lam.framework.operLog.annotation.Log;
import com.lam.framework.operLog.enums.BusinessType;
import com.lam.system.param.SysDictItemParam;
import com.lam.system.service.SysDictItemService;

/**
 * 字典项Controller
 * 
 * @author admin
 * @date 2022-09-19
 */
@Log(title = "字典项")
@ParamInject
@RestController
@RequestMapping("/${adminPath}/sys/dictItem")
public class SysDictItemController extends BaseController {

    @Autowired
    private SysDictItemService sysDictItemService;

    /**
     * 查询字典项列表
     */
    @Log(oper="分页查询")
    @DictAutoTransfer
    @PreAuthorize("@ss.hasPm('sys:dictItem:list')")
    @GetMapping("/list")
    public Result list(SysDictItemParam param) {
    
        startPage();
        List<SysDictItem> list = sysDictItemService.selectList(param);
        return Result.ok(getTableData(list));
    }
    
    /**
     * 获取字典项详细信息
     */
    @DictAutoTransfer
    @Log(oper="根据ID查询")
    @PreAuthorize("@ss.hasPm('sys:dictItem:query')")
    @GetMapping("/{id}")
    public Result getInfo(@PathVariable("id") String id) {
    
        return Result.ok(sysDictItemService.selectById(id));
    }
    
    /**
     * 新增字典项
     */
    @PreAuthorize("@ss.hasPm('sys:dictItem:add')")
    @Log(businessType = BusinessType.INSERT)
    @PostMapping
    public Result add(@Validated(Add.class) @RequestBody SysDictItem sysDictItem) {
    
    	ValidateUtil.validUnique(sysDictItem, ValidOperType.ADD, SystemConstant.UNIQUE_DICT_ITEM_VALUE, "字典项值已存在！");
        return toAjax(sysDictItemService.insert(sysDictItem));
    }
    
    /**
     * 修改字典项
     */
    @PreAuthorize("@ss.hasPm('sys:dictItem:edit')")
    @Log(businessType = BusinessType.UPDATE)
    @PutMapping
    public Result edit(@Validated(Update.class) @RequestBody SysDictItem sysDictItem) {
    
    	ValidateUtil.validUnique(sysDictItem, ValidOperType.UPDATE, SystemConstant.UNIQUE_DICT_ITEM_VALUE, "字典项值已存在！");
        return toAjax(sysDictItemService.update(sysDictItem));
    }
    
    /**
     * 删除字典项
     */
    @PreAuthorize("@ss.hasPm('sys:dictItem:remove')")
    @Log(businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public Result remove(@PathVariable String id) {
    
        return toAjax(sysDictItemService.deleteById(id));
    }
    

}
